$(function(){
	var picker = new mui.PopPicker();
	picker.setData([{
	    value: "1",
	    text: "自己"
	}, {
	    value: "2",
	    text: "父母"
	}, {
	    value: "3",
	    text: "爱人"
	}, {
	    value: "4",
	    text: "子女"
	}, {
	    value: "5",
	    text: "其他"
	}])
	$("#forme").click(function(){
		picker.show(function(item) {
			$("#forme").text(item[0].text).css("color","#333");
			$("input[name='ourself']").val(item[0].value);
		})
	});
	
	var today = new Date();
	var dtpicker = new mui.DtPicker({
	    type: "date",//设置日历初始视图模式 
	    beginDate: new Date(1900, 01, 01),//设置开始日期 
	    endDate: new Date(today.getFullYear(),today.getMonth(),today.getDate()-1),//设置结束日期 
	}) ;
	
    $("#birthday").click(function(){
	    dtpicker.show(function(items) {
	    	$("#birthday").text(items.value).css("color","#333");
	    	$("input[name='birthday']").val(items.value);
		})  
    });
    
    var id = utilModule.getQueryString("id");
	if(id !=null){
		 initqueryElectronicArchivesList();
	}
	
    /*获取电子档案*/
	function initqueryElectronicArchivesList(){
		var param = {"patientid":USERID,"type":2};
		function callBack(data){
			console.log(data);
			$("input[name='name']").val(data.name);
			if(data.sex==2){
				$("input[name='sex']").attr("checked",true);
			}
			if(typeof(data.birthday) != "undefined"){
				$("input[name='birthday']").val(data.birthday);
				$("#birthday").text(data.birthday).css("color","#333");
			}
			if(typeof(data.ourself)  !="undefined"){
				var a  = picker.pickers[0].items;
				console.log(a)
				for (one in a) {
					if(a[one].value == data.ourself){
						$("#forme").text(a[one].text).css("color","#333");
						$("input[name='ourself']").val(data.ourself);
					}
				}
				//
			}
			$("input[name='phoneNumber']").val(data.phoneNumber);

		}
		electronicArchivesMdule.queryElectronicArchivesList(param,callBack);
	};
	$(".html-loding").hide();
	$("#nextBtn").on("click",function(){
		
		//与你的关系
	    var me=$("#forme").text();
		if(me=="请选择"){
			mui.alert("请选择与您的关系!","提示");
			return;
		}
		//真实姓名
	    var name=$("input[name='name']").val();
		if(name.trim()==""){
			mui.alert("请输入您的真实名字!","提示");
			return;
		}
		//出生日期
		var birthday=$("#birthday").text();
		if(birthday=="请选择"){
			mui.alert("请选择出生日期!","提示");
			return;
		}
		//输入手机号
		var phone=$("input[name='phoneNumber']").val();
		if(phone.trim()==""){
			mui.alert("请输入手机号码!","提示");
			return;
		}
	    if(!(/^1[34578]\d{9}$/.test(phone))){ 
	    	mui.alert("您的手机号码有误,请重新输入","提示");  
	        return; 
	    }
		
		var id = utilModule.getQueryString("id");
		if(id !=null){
			var param =$('#thisform').serialize()+"&patientid="+USERID+"&id="+id;
			electronicArchivesMdule.updateElectronicArchives(param,callBack);
		}else{
			var param =$('#thisform').serialize()+"&patientid="+USERID;
			electronicArchivesMdule.insertElectronicArchives(param,callBack);
		}
		function callBack(data){
			console.log(data);
			if(data.status==200||data ==1){
				mui.alert("保存成功","提示",function(){
					location.href="electronic-archives.html";
				}); 
			}else{
				mui.toast("保存失败");
			}
		}
	});
});
	
	
